﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ActivityTreeNode.cs" company="YellowFish">
//   Copyright © YellowFish 2009
// </copyright>
// <summary>
//   Describes and manipulates the activity tree node.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace MobilePal.Views.TreeView
{
  #region Imported Namespaces


  using System;
  using System.Windows.Forms;

  using State;


  #endregion


  /// <summary>
  /// Describes and manipulates the activity tree node.
  /// </summary>
  public class ActivityTreeNode: TreeNode, IActivityTreeNode
  {
    #region Constants and Fields


    private readonly Activity _activity;
    private readonly ActivityNodeSettings _settings;
    private string _inProgressLabelFormat;
    private string _labelFormat;


    #endregion


    #region Constructors and Destructors


    /// <summary>
    /// Initializes a new instance of the <see cref="ActivityTreeNode"/> class.
    /// </summary>
    /// <param name="activity">The activity.</param>
    /// <param name="settings">The settings.</param>
    public ActivityTreeNode(Activity activity, ActivityNodeSettings settings)
    {
      _activity = activity;
      _settings = settings;
      _activity.IsInProgressChanged += ActivityIsInProgressChanged;
      _labelFormat = _inProgressLabelFormat = "{0}";

      Update();
    }


    /// <summary>
    /// Initializes a new instance of the <see cref="ActivityTreeNode"/> class.
    /// </summary>
    /// <param name="activity">The activity.</param>
    public ActivityTreeNode(Activity activity)
      :this(activity, null)
    {
    }


    #endregion


    #region Properties


    /// <summary>
    /// Gets the activity.
    /// </summary>
    /// <value>
    /// The activity.
    /// </value>
    public Activity Activity
    {
      get
      {
        return _activity;
      }
    }


    /// <summary>
    /// Gets or sets the in progress label format.
    /// </summary>
    /// <value>
    /// The in progress label format.
    /// </value>
    public string InProgressLabelFormat
    {
      get
      {
        return _inProgressLabelFormat;
      }

      set
      {
        _inProgressLabelFormat = value;

        UpdateLabel();
      }
    }


    /// <summary>
    /// Gets or sets the label format.
    /// </summary>
    /// <value>
    /// The label format.
    /// </value>
    public string LabelFormat
    {
      get
      {
        return _labelFormat;
      }

      set
      {
        _labelFormat = value;

        UpdateLabel();
      }
    }

    /// <summary>
    /// Gets the settings.
    /// </summary>
    /// <value>
    /// The settings.
    /// </value>
    public ActivityNodeSettingsBase GetSettings()
    {
      _settings.ChildNodesSettings.Clear();

      foreach (IActivityTreeNode node in Nodes)
      {
        _settings.ChildNodesSettings.Add(node.GetSettings());
      }

      return _settings;
    }


    #endregion


    #region Methods


    private void ActivityIsInProgressChanged(object sender, EventArgs e)
    {
      Update();
    }


    private void Update()
    {
      UpdateLabel();
      UpdateCheckBox();
    }


    private void UpdateCheckBox()
    {
      Checked = _activity.IsInProgress;
    }


    private void UpdateLabel()
    {
      string format = _activity.IsInProgress ? InProgressLabelFormat : LabelFormat;

      Text = string.Format(format, _activity.Name, _activity.LastToggleTime);
    }


    #endregion
  }
}